package spotGUI.personalProfileGUI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import personalProfile.PhotoManager;
import personalProfile.PhotoManager.Photo;
import personalProfile.PhotoManager.PhotoAlbum;
import java.util.ResourceBundle;

public class AlbumView extends JPanel implements Observer, ComponentListener {


	private static final int EMPTY_BORDER_THICKNESS  = 10;
	
	private PhotoAlbum album;
	private PhotoManager manager;
	private JLabel lblAlbum;
	private JLabel lblLastUpdate;
	private JPanel photoPanel;
	private JTextArea textArea;
	
	private FlowLayout fl_photoPanel;


	public AlbumView(PhotoAlbum album, PhotoManager manager)  {
	
		
		setBorder(new EmptyBorder(EMPTY_BORDER_THICKNESS, EMPTY_BORDER_THICKNESS, EMPTY_BORDER_THICKNESS, EMPTY_BORDER_THICKNESS));

		this.manager = manager;
		this.album = album;
		manager.addObserver(this);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		lblAlbum = new JLabel("album");
		lblAlbum.setText(album.name);
		lblAlbum.setFont(new Font("Agency FB", Font.BOLD, 23));
		GridBagConstraints gbc_lblAlbum = new GridBagConstraints();
		gbc_lblAlbum.anchor = GridBagConstraints.WEST;
		gbc_lblAlbum.insets = new Insets(0, 0, 5, 0);
		gbc_lblAlbum.gridx = 0;
		gbc_lblAlbum.gridy = 0;
		add(lblAlbum, gbc_lblAlbum);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("Tahoma", Font.ITALIC, 13));
		textArea.setText(album.description);
		textArea.setEditable(false);
		textArea.setOpaque(false);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.insets = new Insets(0, 0, 5, 0);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 0;
		gbc_textArea.gridy = 1;
		add(textArea, gbc_textArea);
		
		lblLastUpdate = new JLabel("");
		if (album.lastUpdate != null)
			lblLastUpdate.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AlbumView.lblLastUpdate.text")+DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(album.lastUpdate));
		else
			lblLastUpdate.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AlbumView.lblLastUpdate.text")+"/");
		GridBagConstraints gbc_lblLastUpdate = new GridBagConstraints();
		gbc_lblLastUpdate.anchor = GridBagConstraints.WEST;
		gbc_lblLastUpdate.insets = new Insets(0, 0, 5, 0);
		gbc_lblLastUpdate.gridx = 0;
		gbc_lblLastUpdate.gridy = 2;
		add(lblLastUpdate, gbc_lblLastUpdate);
		
		photoPanel = new JPanel();
		fl_photoPanel = (FlowLayout) photoPanel.getLayout();
		fl_photoPanel.setAlignOnBaseline(true);
		fl_photoPanel.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_photoPanel = new GridBagConstraints();
		gbc_photoPanel.fill = GridBagConstraints.BOTH;
		gbc_photoPanel.gridx = 0;
		gbc_photoPanel.gridy = 3;
		add(photoPanel, gbc_photoPanel);
		update(null, true);
	}


	public void enablePhotoRemoval() {
		for (int i = 0; i < photoPanel.getComponentCount(); i++) {
			((LittlePhotoView) photoPanel.getComponent(i)).enablePhotoRemoval();
		}

	}

	@Override
	public void update(Observable arg0, Object bigViewer) {
		photoPanel.removeAll();
		ArrayList<Photo> photos = album.photos;
		for (int i = 0; i < photos.size(); i++) {
			if (bigViewer instanceof Boolean)
				photoPanel.add(new LittlePhotoView(photos.get(i), manager, (Boolean)bigViewer));
			else
				photoPanel.add(new LittlePhotoView(photos.get(i), manager, true));
		}
		repaint();
		
	}


	private int calculateHeight(int newWidth) {
		int lineHeight;
		try {
			lineHeight = photoPanel.getComponent(0).getHeight()+ fl_photoPanel.getVgap();
		} catch (IndexOutOfBoundsException e) {
			lineHeight = 0;
		}
		int hGap = fl_photoPanel.getHgap();
		
		int currentLineWidth = EMPTY_BORDER_THICKNESS*2 + hGap;
		int totalHeight = lblAlbum.getHeight()+textArea.getHeight()+lblLastUpdate.getHeight()+EMPTY_BORDER_THICKNESS*2+15;
		
		totalHeight += lineHeight;
		for (int i = 0; i < photoPanel.getComponentCount(); i++) {
			currentLineWidth += (photoPanel.getComponent(i).getWidth() + hGap );
			if (currentLineWidth >= newWidth) {
				totalHeight += lineHeight;
				currentLineWidth = photoPanel.getComponent(i).getWidth() + EMPTY_BORDER_THICKNESS*2 + hGap;
			}
		}
		return totalHeight;
	}


	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void componentResized(ComponentEvent arg0) {
		int newWidth = ((JScrollPane)arg0.getSource()).getViewport().getWidth();
		setPreferredSize(new Dimension(newWidth, calculateHeight(newWidth)));
	}


	@Override
	public void componentShown(ComponentEvent arg0) {
		componentResized(arg0);
	}

}
